package com.jsy.mqhelper.charlotterpc.config;

import com.jsy.mqhelper.charlotterpc.config.mq.RabbitMQProperties;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author SongyangJi
 * @date 2022/08/15
 */
public class CharlottePropertyManager implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;


    private static <T> T getBean(Class<T> requiredType) {
        return (T) applicationContext.getBean(requiredType);
    }

    public static CharlotteProperties getCharlotteProperties() {
        return getBean(CharlotteProperties.class);
    }

    public static RabbitMQProperties getRabbitMQProperties() {
        return getBean(RabbitMQProperties.class);
    }

    /**
     * Spring容器启动后，会把 applicationContext 给自动注入进来，然后我们把 applicationContext 赋值到静态变量中，方便后续拿到容器对象
     *
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CharlottePropertyManager.applicationContext = applicationContext;
    }
}
